<template>
  <div>
    <el-dialog
      :visible.sync="isVisible"
      title="添加用户"
      center
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="editForm"
        size="mini"
        label-position="left"
        label-width="60px"
        status-icon
        :model="editForm"
        :rules="editFormRules"
      >
        <el-form-item label="账号" prop="username">
          <el-col :span="21">
            <el-input v-model="editForm.username" autocomplete="off" placeholder="账号" :span="21" />
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-col :span="21">
            <el-input v-model="editForm.password" autocomplete="off" placeholder="密码" />
          </el-col>
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-col :span="21">
            <el-input v-model="editForm.nickname" autocomplete="off" placeholder="昵称" />
          </el-col>
        </el-form-item>
        <el-form-item label="姓名" prop="reallyName">
          <el-col :span="21">
            <el-input v-model="editForm.reallyName" autocomplete="off" placeholder="姓名" />
          </el-col>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-col :span="21">
            <el-input v-model="editForm.avatar" autocomplete="off" placeholder="头像" />
          </el-col>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-col :span="21">
            <el-input v-model="editForm.phone" autocomplete="off" placeholder="电话" />
          </el-col>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-col :span="21">
            <el-input v-model="editForm.email" autocomplete="off" placeholder="邮箱" />
          </el-col>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-col :span="21">
            <el-radio-group v-model="editForm.sex">
              <el-radio :label="0">
                男
              </el-radio>
              <el-radio :label="1">
                女
              </el-radio>
            </el-radio-group>
          </el-col>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-col :span="21">
            <el-input v-model="editForm.age" autocomplete="off" placeholder="年龄" />
          </el-col>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-col :span="21">
            <el-select v-model="editForm.status" placeholder="请选择状态">
              <el-option label="正常" :value="0" />
              <el-option label="冻结" :value="1" />
              <el-option label="删除" :value="2" />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="角色" prop="roleIds">
          <el-col :span="21">
            <el-select v-model="editForm.roleIds" multiple placeholder="请选择角色" value-key="id" @change="handleChange">
              <el-option
                v-for="item in roleList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
          <el-col :span="3">
            <el-checkbox v-model="checked" style="margin-left: 12px" @change="handleAllChecked">
              全选
            </el-checkbox>
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">
          取 消
        </el-button>
        <el-button
          type="primary"
          size="mini"
          :loading="loading"
          @click="handleEdit('editForm')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'UserAdd',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checked: false,
      roleList: [],
      loading: false,
      editForm: {
        username: '',
        password: '',
        nickname: '',
        reallyName: '',
        avatar: '',
        phone: '',
        email: '',
        sex: '',
        age: '',
        status: '',
        roleIds: []
      },
      editFormRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        reallyName: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        avatar: [
          { required: true, message: '请输入头像', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择状态', trigger: 'blur' }
        ],
        roleIds: [
          { required: true, message: '请选择角色', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    isVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    isVisible(val) {
      if (val) {
        this.$nextTick(() => {
          this.checked = false
          this.$refs['editForm'].resetFields()
        })
        this.listRoles()
      }
    }
  },
  methods: {
    listRoles() {
      this.AjaxUtils.get('/role/listRole').then((response) => {
        const res = response.data
        this.roleList = []
        res.forEach(item => {
          this.roleList.push({ value: item.id, label: item.name })
        })
      }).catch(() => {
      })
    },
    handleAllChecked(val) {
      if (val) {
        this.editForm.roleIds = []
        this.roleList.forEach(item => {
          this.editForm.roleIds.push(item.value)
        })
      } else {
        this.editForm.roleIds = []
      }
    },
    handleChange(val) {
      this.checked = val.length === this.roleList.length
    },
    handleEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.editForm)
          this.loading = true
          this.AjaxUtils.post('/user/insertUser', this.editForm, true).then((response) => {
            this.loading = false
            this.isVisible = false
            this.$emit('reloadTable')
            console.log(response)
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-select {
    width: 100%;
  }
</style>
